<?php

class CuentasDespliegueController extends Sgit_Controller_Action
{


    protected $_checkZona = true;
    protected $_tipo = 'Despliegue';
    protected $_setParams = array('idZona'=>49);

    public function _begin()
    {
        $this->anio = $this->_request->getParam('anio');
        $this->version = $this->_request->getParam('version', 0);
        $this->view->version=$version;
        
        if ($this->_idZona == 10)
        {
            $this->zonas = array(1, 3, 4, 6);
        } elseif ($this->_idZona == 18)
        {
            $this->zonas = array(1, 2, 3, 4, 6);
        } else
        {
            $this->zonas = $this->_idZona;
        }

    }

    public function indexAction()
    {
        
        $anio = $this->anio;
        $version = $this->version;
        $zonas=$this->zonas;

        $filtros = new Sgit_Form_Filtros();
        $f_anio = new Sgit_Form_Element_Select_Year('anio');
        $f_anio->setValue($anio);
        $filtros->addFormSelect($f_anio);

        $f_version = new Sgit_Form_Element_Select_Version('version', array('firstBlank' => false));
        $f_version->setValue($version);
        $filtros->addFormSelect($f_version);
        $filtros->addPdf();
        $filtros->addExcel();
        $values = $this->_getFiltrosValues($filtros);
        $filtros->myRender();


 
        $zonas = $this->_idZona;
        


        if ($anio)
        {
            $this->_toExport(true);
            
            $cuentas = new Application_Model_Cuentas();
            $cuentas->setAnio($anio);
            $cuentas->setVersion($version);
            $cuentas->setZona($this->_idZona);
            $this->view->anio = $anio;



            $this->view->coste_sap =    $cuentas->addInformeDespliegue($cuentas->getCosteSapDespliegue());
            $this->view->inversa_sap =  $cuentas->addInformeDespliegue($cuentas->getInversaSapDespliegue());
            $this->view->saldo_sap =    $cuentas->addInformeDespliegue($cuentas->getSaldoSapDespliegue());


            $this->view->inmovilizado_manoobra = $cuentas->addInformeDespliegue($cuentas->getSerieNaturalezaMeses(45));
            $this->view->inmovilizado_materiales = $cuentas->addInformeDespliegue($cuentas->getSerieNaturalezaMeses(46));
            $this->view->inmovilizado_total = $cuentas->addInformeDespliegue($cuentas->getInmovilizadoDespliegue());


            $meses = Sgit_Util_Meses::run();

            foreach ($meses as $k => $m)
            {
                $ico[$k] = substr($m, 0, 3);
            }

            $this->view->meses = $ico;
        }
    }

    public function avanceAction()
    {
        $anio = $this->anio;
        $this->version = 0;
        $zonas=$this->zonas;
        $this->view->version=$this->version;


        $filtros = new Sgit_Form_Filtros();
        $f_anio = new Sgit_Form_Element_Select_Year('anio');
        $f_anio->setValue($anio);
        $filtros->addFormSelect($f_anio);


        
        $filtros->addPdf();
        $filtros->addExcel();
        $values = $this->_getFiltrosValues($filtros);
        $filtros->myRender();


        if ($anio)
        {
            $this->_toExport(true);
            
            $cuentas = new Application_Model_Cuentas();
            $cuentas->setAnio($anio);
            $cuentas->setVersion($this->version);
            $cuentas->setZona($this->_idZona);
            $this->view->anio = $anio;


            $estados = new Application_Model_CuentasEstados();
            $estados->setZona($this->_idZona);
            $estados->setAnio($anio);

            $estado = $estados->getEstados($this->_tipo);

            $this->view->estados = $estado;
            $this->view->sap_coste = $cuentas->addAvance($cuentas->getSerieNaturalezaMeses(43));
            $this->view->sap_inversa = $cuentas->addAvance($cuentas->getSerieNaturalezaMeses(44));
    
            $this->view->inmovilizado_manoobra = $cuentas->addAvance($cuentas->getSerieNaturalezaMeses(45));
            $this->view->inmovilizado_materiales = $cuentas->addAvance($cuentas->getSerieNaturalezaMeses(46));
           
            $this->view->meses = Sgit_Util_Meses::run();

        }
    }
    
    

    public function editarAction()
    {

        $tipo = $this->_request->getParam('tipo');

        $keys['id_zona'] = $this->_request->getParam('idZona');
        $keys['mes'] = $this->_request->getParam('mes');
        $keys['anio'] = $this->_request->getParam('anio');
        $keys['id_naturaleza'] = $this->_request->getParam('id_naturaleza');
        $keys['version'] = $this->_request->getParam('version');

        $dato = Sgit_Filter_FloatNull::filter($this->_request->getParam('dato'));
        $fecha = date('Y-m-d');

        if ($tipo == 'presupuesto')
        {
            $forUpdate['f_presupuesto'] = $fecha;
            $forUpdate['presupuesto'] = $dato;
        } elseif ($tipo == 'estimado')
        {
            $forUpdate['f_estimado'] = $fecha;
            $forUpdate['estimado'] = $dato;
        }


        $cuentas = new Application_Model_Cuentas();
        $cuentas->replace($keys, $forUpdate);

        $where['id_zona=?'] = $keys['id_zona'];
        $where['id_naturaleza=?'] = $keys['id_naturaleza'];
        $where['anio=?'] = $keys['anio'];
        $where['version=?'] = $keys['version'];


        $tipo_quote = "SUM(" . $cuentas->getAdapter()->quoteIdentifier($tipo) . ")";
        $suma = $cuentas->getOne($tipo_quote, $where);

        $json['status'] = 'ok';
        $json['suma'] = $this->view->numEs($suma, 0);


        $this->_helper->json($json);

    }


    public function lockAction()
    {

        $keys['id_zona'] = $this->_request->getParam('idZona');
        $keys['mes'] = $this->_request->getParam('mes');
        $keys['anio'] = $this->_request->getParam('anio');
        $keys['version'] = $this->_request->getParam('version');
        $keys['tipo'] = $this->_tipo;

        $forUpdate['objeto_cambio'] = $this->_request->getParam('objeto_cambio');
        $forUpdate['notas_cambio'] = $this->_request->getParam('notas');
        $forUpdate['cambio_aceptado'] = null;
        $forUpdate['estado'] = 0;
        $forUpdate['propone_cambio'] = $this->_auth->getIdentity()->id;
        $forUpdate['fecha_peticion_cambio'] = date('Y-m-d');
        $forUpdate['fecha_cierre'] = date('Y-m-d');


        $cuentas = new Application_Model_CuentasEstados();
        $cuentas->replace($keys, $forUpdate);

        $json['status'] = 'ok';
        $this->_helper->json($json);
    }

    public function valuesToExport()
    {
        $cuentas = new Application_Model_Cuentas();
        $cuentas->setAnio($this->anio);
        $cuentas->setVersion($this->version);
        $cuentas->setZona($this->zonas);
            
            
            
        foreach($cuentas->addExport($cuentas->getCosteSapDespliegue()) as $f) $rows[]=$f;
        foreach($cuentas->addExport($cuentas->getInversaSapDespliegue()) as $f) $rows[]=$f;
        foreach($cuentas->addExport($cuentas->getSaldoSapDespliegue()) as $f) $rows[]=$f;    
        foreach($cuentas->addExport($cuentas->getSerieNaturalezaMeses(45)) as $f) $rows[]=$f;
        foreach($cuentas->addExport($cuentas->getSerieNaturalezaMeses(46)) as $f) $rows[]=$f;
        foreach($cuentas->addExport($cuentas->getInmovilizadoDespliegue()) as $f) $rows[]=$f;
     
        
        return $rows;
    }
    
    public function excelAction()
    {
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);

        $excel = new Sgit_Excel();
        $excel->setColumnas($this->_getXml());
        $excel->render($this->valuesToExport(), $this->_controllerName);
    }
    
    public function pdfAction()
    {
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);

        $pdf = new Sgit_Pdf('L');
        $pdf->setColumnas($this->_getXml());
        
        $pdf->SetTitle('Despliegue IT '.$this->zona.' '.$this->anio.' V'.$this->version);
        $pdf->render($this->valuesToExport(), $this->_controllerName);
            
    }



}
